package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xpvideo.entity.China;
import com.example.xpvideo.entity.Order;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.BackOrderReq;
import com.example.xpvideo.entity.res.BackstageOrderListRes;
import com.example.xpvideo.entity.res.OrderDetailsRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.mapper.OrderMapper;
import com.example.xpvideo.service.ChinaService;
import com.example.xpvideo.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ChinaService chinaService;

    @Override
    public ResponseResult<PageResult<List<BackstageOrderListRes>>> backOrderList(BackOrderReq backOrderReq) {
        PageHelper.startPage(backOrderReq.getPage(), backOrderReq.getSize());
        List<BackstageOrderListRes> list = orderMapper.backOrderList(backOrderReq);
        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<BackstageOrderListRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);
        return ResponseResult.responseResult(0, listPageResult);
    }

    /**
     * 查询订单详情
     * @param orderSn
     * @return
     */
    @Override
    public ResponseResult orderDetailsInfo(Integer orderSn) {

        OrderDetailsRes orderDatailsRes = orderMapper.orderDetailsInfo(orderSn);
        China china = chinaService.getOne(new QueryWrapper<China>().eq("id", orderDatailsRes.getProvince()));// 省
        China china2 =chinaService.getOne(new QueryWrapper<China>().eq("id", orderDatailsRes.getCity())); // 市
        China china3 =chinaService.getOne(new QueryWrapper<China>().eq("id", orderDatailsRes.getDistrict())); // 区
        orderDatailsRes.setProvinces(china.getName()+" "+china2.getName()+" "+china3.getName());

        List<Map<String,Object>> goods = orderMapper.queryOrderGoodsInfo(orderDatailsRes.getId());
        orderDatailsRes.setGoods(goods);

        if (null == orderDatailsRes) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_20001, null);
        }
        return ResponseResult.responseResult(0, orderDatailsRes);
    }
}




